﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace msBsc.WebUi
{
    public partial class AddNewOrders : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public override void DoAction()
        {
            if (this.strAction == "AddOrderInfo")
            {
                AddNewOrderInfo();
            }

            if (this.strAction == "GetDefaultInfo")
            {
                GetDefaultInfo();
            }
        }

        /// <summary>
        /// 获取默认显示的车辆信息
        /// </summary>
        private void GetDefaultInfo()
        {
            string vehicleId = this.GetParamHtValue("VehicleId");

            if (string.IsNullOrWhiteSpace(vehicleId))
            {
                this.ExecJScript(@"ShowDefaultInfo(""N"")");
                return;
            }

            System.Data.DataSet ds = null;
            using (Epm.AppLogic.Framework.CDbAccess objCDbAccess = new Epm.AppLogic.Framework.CDbAccess())
            {
                Epm.AppLogic.Framework.CDbParmCollection parmCollection = new Epm.AppLogic.Framework.CDbParmCollection();
                parmCollection.Add(new Epm.AppLogic.Framework.CDbParm("vehicleId", System.Data.ParameterDirection.Input, Epm.AppLogic.Framework.EDbType.String, 36, vehicleId));

                ds = objCDbAccess.QueryBySQL(@"SELECT T1.*,D.CUSTOMER_BASEINFO_NAME AGENT_NAME FROM
                                            (SELECT T.*,B.CUSTOMER_BASEINFO_NAME DJK_NAME,B.CUSTOMER_BASEINFO_MOBILE DJK_PHONE,C.FACCOUNT_ID FROM 
                                            (SELECT A.VEHICLE_NAME,A.DJACK_ID,A.LICENSE_PLATE_NUMBER 
                                            FROM DJK_VEHICLE_INFO A
                                            WHERE A.VEHICLE_INFO_ID=@vehicleId) T 
                                            LEFT JOIN DJK_CUSTOMER_BASEINFO B ON T.DJACK_ID=B.CUSTOMER_BASEINFO_ID
                                            LEFT JOIN DJK_CUSTOMER_REGISTER C ON T.DJACK_ID=C.CUSTOMER_REGISTER_ID) T1
                                            LEFT JOIN DJK_CUSTOMER_BASEINFO D ON               T1.FACCOUNT_ID=D.CUSTOMER_BASEINFO_ID", parmCollection);
            }

            if (ds == null
                || ds.Tables[0].Rows.Count <= 0)
            {
                this.ExecJScript(@"ShowDefaultInfo(""N"")");
                return;
            }

            this.ExecJScript(@"ShowDefaultInfo(""Y"",'" + ds.Tables[0].Rows[0]["VEHICLE_NAME"] + "','" + ds.Tables[0].Rows[0]["LICENSE_PLATE_NUMBER"] + "','" + ds.Tables[0].Rows[0]["DJK_NAME"] + "','" + ds.Tables[0].Rows[0]["DJK_PHONE"] + "','" + ds.Tables[0].Rows[0]["AGENT_NAME"] + "')");
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        private void AddNewOrderInfo()
        {
            string buyerName = this.GetParamHtValue("buyerName");
            string buyerGender = this.GetParamHtValue("buyerGender");
            string telPhone = this.GetParamHtValue("telPhone");
            string startTime = this.GetParamHtValue("startTime");
            string stopTime = this.GetParamHtValue("stopTime");
            string orderVal = this.GetParamHtValue("orderVal");
            string totalVal = this.GetParamHtValue("totalVal");
            string sourceType = this.GetParamHtValue("sourceType");
            string sourceCode = this.GetParamHtValue("sourceCode");
            string orderRemark = this.GetParamHtValue("orderRemark");
            string VehicleId = this.GetParamHtValue("VehicleId");

            decimal orderFee, totalFee;
            DateTime s_dt, e_dt;

            if (!decimal.TryParse(orderVal, out orderFee))
            {
                this.Alert("定金金额有误");
                return;
            }
            if (!decimal.TryParse(totalVal, out totalFee))
            {
                this.Alert("定单总额有误");
                return;
            }
            if (!DateTime.TryParse(startTime, out s_dt))
            {
                this.Alert("开始服务时间有误");
                return;
            }
            if (!DateTime.TryParse(stopTime, out e_dt))
            {
                this.Alert("结束服务时间有误");
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizDiversionOrder objCBizDiversionOrder = new AppLogic.DataAlc.CBizDiversionOrder())
            {
                string[] result = objCBizDiversionOrder.AddNewOrderInfo(VehicleId, buyerName, buyerGender, orderFee, totalFee, objCBizDiversionOrder.dtCurrDateTime, s_dt, e_dt, telPhone, sourceType, sourceCode, orderRemark);

                if (result[0] != "TRUE")
                {
                    this.Alert(result[1]);
                    return;
                }

                this.ExecJScript(@"AddSuccess()");
            }
        }
    }
}